package com.x.erp.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.x.erp.model.Brand;
import com.x.framework.dao.BaseDao;
@Repository
public class BrandDao extends BaseDao {
	private final static String selectBrandListSql = "SELECT a.BRAND_ID , BRAND_NAME , BRAND_PINYIN , BRAND_ENGLISH_NAME , BRAND_INITIAL , BRAND_LOGO , COUNTRY_ID , zs " +
            "FROM x_base.BASE_BRAND AS a " +
            "LEFT JOIN( SELECT c.BRAND_ID , count(1) AS zs FROM x_base.third_brand_check AS c WHERE c.BRAND_PASS = 0 GROUP BY c.BRAND_ID ) AS b ON a.BRAND_ID = b.BRAND_ID " +
            "WHERE a.BRAND_ID > 0 ";
	public List<Brand> selectBrandList(Brand brand) throws Exception{
		StringBuilder sb = new StringBuilder(selectBrandListSql);
		List<Object> params = new ArrayList<Object>();
		if(brand.getBrandId() != null){
			sb.append("AND a.BRAND_ID = ? ");
			params.add(brand.getBrandId());
		}
		if(this.isString(brand.getBrandName())){
			sb.append("AND BRAND_NAME = ? ");
			params.add(brand.getBrandName());
		}
		if(this.isString(brand.getBrandPinyin())){
			sb.append("AND BRAND_PINYIN = ? ");
			params.add(brand.getBrandPinyin());
		}
		if(this.isString(brand.getBrandEnglishName())){
			sb.append("AND BRAND_ENGLISH_NAME = ? ");
			params.add(brand.getBrandEnglishName());
		}
		if(this.isString(brand.getBrandInitial())){
			sb.append("AND BRAND_FIRST_LETTER = ? ");
			params.add(brand.getBrandInitial());
		}
		if(brand.getCountryId() != null){
			sb.append("AND COUNTRY_ID = ? ");
			params.add(brand.getCountryId());
		}
		sb.append("ORDER BY BRAND_PINYIN ");
        if(brand.getTruePage() != null){
            this.getLimitSql(sb, brand) ;
        }
		return this.queryForList(sb.toString(), Brand.class, params);
	}

    /**
     * 添加
     */
    private final static String insertBrandListSql = "INSERT INTO x_base.BASE_BRAND(BRAND_ID,BRAND_NAME,BRAND_PINYIN,BRAND_ENGLISH_NAME,BRAND_INITIAL,BRAND_LOGO,COUNTRY_ID) VALUES ";

    public void insertBrandList(Brand brand) throws Exception{
        StringBuilder sql = new StringBuilder(insertBrandListSql);
        List<Object> params = new ArrayList<>();
        if(brand.getBrandLogo() != null){
            StringBuffer img = new StringBuffer().append("https://ssl-assets.che300.com/theme/images/brand/large/b").append(brand.getBrandId().toString()).append(".jpg?imageMogr2/thumbnail/x100") ;
            brand.setBrandLogo(img.toString());
        }

        if(brand != null){
            sql.append("(?,?,?,?,?,?,?)") ;
            params.add(brand.getBrandId());
            params.add(brand.getBrandName());
            params.add(brand.getBrandPinyin()) ;
            params.add(brand.getBrandEnglishName()) ;
            params.add(brand.getBrandInitial());
            params.add(brand.getBrandLogo());
            params.add(brand.getCountryId());
            this.execute(sql.toString(), params);
        }
    }

    /**
     * 修改
     */
    public void updateBrandList(Brand brand) throws Exception{
        if(brand != null){
            StringBuilder sql = new StringBuilder();
            List<Object> params = new ArrayList<>();
            sql.append("UPDATE x_base.BASE_BRAND set BRAND_NAME=?, BRAND_PINYIN=?, BRAND_INITIAL=?,BRAND_LOGO=? where BRAND_ID = ? ") ;
            params.add(brand.getBrandName());
            params.add(brand.getBrandPinyin()) ;
            params.add(brand.getBrandInitial());
            params.add(brand.getBrandLogo());
            params.add(brand.getBrandId());
            this.execute(sql.toString(), params);
        }
    }

    /**
     * 按照brand id 查询
     */
    public Brand getSimpleBrand(Map<String, Object> brand) throws Exception{
        StringBuffer sql = new StringBuffer();
        sql.append("SELECT BRAND_ID , BRAND_NAME , BRAND_PINYIN , BRAND_ENGLISH_NAME , BRAND_INITIAL , BRAND_LOGO , COUNTRY_ID"
        +" FROM x_base.BASE_BRAND where true ") ;
        List<Object> params = new ArrayList<>();
        if(brand.containsKey("brandId")){
            params.add(brand.get("brandId"));
            sql.append(" and BRAND_ID = ? ") ;
        }
        if(brand.containsKey("brandName")){
            params.add(brand.get("brandName"));
            sql.append(" and BRAND_NAME = ? ") ;
        }

        return (Brand) this.queryForObject(sql.toString(), Brand.class, params);
    }

    /**
     * 分页Sql
     * @param sql
     * @param brand)
     */
    private void getLimitSql(StringBuilder sql, Brand brand) {
        if(brand.getPageSize() != null && brand.getPageSize() > 0) {
            sql.append(" LIMIT ").append(brand.getPageIndex()-1).append(",").append(brand.getPageSize());
        } else {
            sql.append(" LIMIT 5");
        }
    }
}
